package com.aruba.download.controller;

import com.aruba.download.bean.Player;
import com.aruba.download.service.PlayerService;
import com.aruba.download.service.UploadService;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
public class PlayerController {
    // 文件存储位置
    private final static String FILESERVER = "http://192.168.42.4:8888/";
    @Autowired
    private PlayerService playerService;
    @Autowired
    private UploadService uploadService;

    @RequestMapping("registerPlayer.do")
    @ResponseBody
    public String register(Player player) {
        System.out.println(player);
        Integer rows = playerService.addPlayer(player);
        if (rows != null && rows > 0)
            return "success";

        return "failed";
    }

    @RequestMapping("findAllPlayer.do")
    @ResponseBody
    public List<Player> findAllPlayer() {
        List<Player> allPlayers = playerService.findAllPlayers();
        System.out.println(allPlayers);
        return allPlayers;
    }

    @RequestMapping("download.do")
    public void download(String photo, String filetype, HttpServletResponse response) throws IOException {
        // 设置响应头
        // 告诉浏览器要将数据保存到磁盘上,不在浏览器上直接解析
        response.setHeader("Content-Disposition", "attachment;filename=" + photo);
        // 告诉浏览下载的文件类型
        response.setContentType(filetype);
        // 获取一个文件的输入流
        InputStream inputStream = new URL(photo).openStream();
        // 获取一个指向浏览器的输出流
        ServletOutputStream outputStream = response.getOutputStream();
        // 向浏览器响应文件即可
        IOUtils.copy(inputStream, outputStream);
    }

    @RequestMapping("uploadImg.do")
    @ResponseBody
    public Map<String, Object> uploadImg(MultipartFile img, HttpServletRequest req) throws IOException {
        Map<String, Object> model = new HashMap<>();

        System.out.println("OriginalFilename：" + img.getOriginalFilename());
        String suffix = img.getOriginalFilename().substring(img.getOriginalFilename().lastIndexOf('.'));
        System.out.println("suffix：" + suffix);
        if (!suffix.equalsIgnoreCase(".jpg")) {
            model.put("msg", "文件类型必须为jpg");
            return model;
        }

        String path = uploadService.upload(img);

        model.put("msg", "上传成功");
        model.put("filepath", FILESERVER + path);
        model.put("filetype", img.getContentType());

        System.out.println("返回结果：" + model);

        return model;
    }

    @RequestMapping("/{html}")
    public String register(@PathVariable("html") String html) {
        return html;
    }

    @RequestMapping("registerPlayer2.do")
    @ResponseBody
    public String register2(Player player, @RequestParam("photo1") MultipartFile head, @RequestParam("photo2") MultipartFile[] photos) {
        System.out.println(player);
        Integer rows = playerService.addPlayer(player);
        if (rows != null && rows > 0)
            return "success";

        return "failed";
    }
}
